import { notification } from 'antd';

export const hasPermission = permission => {
  let permissions = window.permissions;

  if(typeof(permissions) == "object"){
    if (permissions.indexOf(permission) > -1) {
      return true;
    }
  }

  if (parent != null &&  parent.permissions != null && parent.permissions != undefined && parent.permissions.indexOf(permission) > -1) {
    return true;
  }

  if(parent.opener != null && typeof(parent.opener.parent.permissions)=="object"){
    if (parent.opener.parent.permissions.indexOf(permission) > -1) {
      return true;
    }else{
      return false;
    }
  }

  if (parent != null && parent.opener != null && parent.opener.permissions != null && parent.opener.permissions != undefined && parent.opener.permissions.indexOf(permission) > -1) {
    return true;
  }

  if (parent != null && parent.opener != null && parent.opener.parent != null && parent.opener.parent.parent != null
    && parent.opener.parent.parent.permissions != null && parent.opener.parent.parent.permissions != undefined
    && parent.opener.parent.parent.permissions.indexOf(permission) > -1) {
    return true;
  }

  if(parent != null && parent.parent != null && parent.parent.parent != null && parent.parent.parent.parent != null && parent.parent.parent.parent.opener != null
    && parent.parent.parent.parent.opener.parent != null && parent.parent.parent.parent.opener.parent.permissions != null
    && parent.parent.parent.parent.opener.parent.permissions != undefined
    && parent.parent.parent.parent.opener.parent.permissions.indexOf(permission) > -1){
    return true;
  }

  if(opener != null && opener.parent != null && opener.parent.opener != null && opener.parent.opener.parent != null
    && opener.parent.opener.parent.permissions != null && opener.parent.opener.parent.permissions != undefined
    && opener.parent.opener.parent.permissions.indexOf(permission) > -1){
    return true;
  }

  if(opener != null && opener.opener != null && opener.opener.parent != null && opener.opener.parent.opener != null
    && opener.opener.parent.opener.parent != null && opener.opener.parent.opener.parent.parent != null
    && opener.opener.parent.opener.parent.parent.permissions != null
    && opener.opener.parent.opener.parent.parent.permissions != undefined
    && opener.opener.parent.opener.parent.parent.permissions.indexOf(permission) > -1){
    return true;
  }

  if(opener != null && opener.parent != null && opener.parent.opener != null && opener.parent.opener.opener != null
    && opener.parent.opener.opener.parent != null && opener.parent.opener.opener.parent.opener != null
    && opener.parent.opener.opener.parent.opener.parent != null
    && opener.parent.opener.opener.parent.opener.parent.permissions != null
    && opener.parent.opener.opener.parent.opener.parent.permissions != undefined
    && opener.parent.opener.opener.parent.opener.parent.permissions.indexOf(permission) > -1){
    return true;
  }

  if (window.parent.permissions != undefined) {
    if (window.parent.permissions.indexOf(permission) > -1) {
      return true;
    }
    else {
      return false;
    }
  } else if (window.opener != undefined && window.opener.parent != undefined && window.opener.parent != null && window.opener.parent.permissions != undefined && window.opener.parent.permissions != null && window.opener.parent.permissions.indexOf(permission) > -1) { //弹出窗口的权限判断
    return true;
  } else if (window.opener != undefined && window.opener.opener != undefined && window.opener.opener.parent.permissions != undefined && window.opener.opener.parent.permissions.indexOf(permission) > -1) {
    return true;
  } else {
    return false
  }
}

export const nopermission = () => {
  notification.warning({
    description: '您的权限不足！请联系管理员',
    message: '权限不足',
  });
}
